// #ifdef MP-QQ
import config from '../../config.js';
import request from '../request.js';
import common from '../common.js';


let mpQq = new Object()

/*
 * 小程序获取OPENID
 * 2021-07-01
 */
mpQq.getOpenid = function() {
	let openid = uni.getStorageSync('openid')
	let user_id = uni.getStorageSync('user_id')

	return new Promise((resolve, reject) => {

		if (!openid) {
			uni.getProvider({
				service: 'oauth',
				success: function(res) {
					if (~res.provider.indexOf('qq')) {
						uni.login({
							provider: 'qq',
							success: function(loginRes) {
								request.getData('base/mpqq/api/Auth/getOpenid', {
									code: loginRes.code
								}, {
									loading: 1
								}).then(res => {
									uni.setStorageSync('openid', res.data
										.openid)
									uni.setStorageSync('user_id', res.data
										.user_id)
									uni.setStorageSync('access_token', res
										.data.access_token)
									uni.setStorageSync(
										'access_token_expire_time', res
										.data.access_token_expire_time)

									uni.getSetting({
										success: (res) => {
											if (res.authSetting[
													'scope.userInfo'
													]) { //已经开启授权
												uni.checkSession({
													success: (
														res
														) => {
														// 登录态未过期，可以直接调用 getUserInfo 获取头像昵称
														uni.getUserInfo({
															provider: 'qq',
															success: (
																res
																) => {
																request
																	.getData(
																		'base/mpqq/api/Auth/userInfoUpdate', {
																			openid: openid,
																			nickname: res
																				.userInfo
																				.nickName,
																			headimgurl: res
																				.userInfo
																				.avatarUrl,
																			country: res
																				.userInfo
																				.country,
																			province: res
																				.userInfo
																				.province,
																			city: res
																				.userInfo
																				.city,
																			gender: res
																				.userInfo
																				.gender,
																			language: res
																				.userInfo
																				.language,
																			iv: res
																				.iv,
																			encryptedData: res
																				.encryptedData,
																			rawData: res
																				.rawData,
																			signature: res
																				.signature,
																		},
																		(
																			res) => {
																			this.$emit(
																				'login'
																				)
																		}
																		);
															}
														});
													}
												});
											}
										}
									});
									resolve({
										openid: uni.getStorageSync(
											'openid'),
										user_id: uni.getStorageSync(
											'user_id'),
									})
									console.log('生产环境刚获得 缓存:openid = ' + uni
										.getStorageSync('openid') +
										' user_id = ' + uni
										.getStorageSync('user_id'))
								}, err => {
									console.log(err)
									reject()
								})
							}
						})
					}
				}
			})
		} else {
			resolve({
				openid: uni.getStorageSync('openid'),
				user_id: uni.getStorageSync('user_id'),
			})
			console.log('生产环境 缓存:openid = ' + uni.getStorageSync('openid') + ' user_id = ' + uni
				.getStorageSync('user_id'))
		}

	})
}

export default mpQq
// #endif